package com.example.sky20.dao;

import com.example.sky20.Bean.AdminBean;
import com.example.sky20.util.DBConnection;
import com.example.sky20.util.Time;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AdminDao {

    DBConnection DBConn = new DBConnection();

    public boolean Checklogin(String s1, String s2) throws SQLException {
        Connection conn = DBConn.getConn();
        Statement stmt = conn.createStatement();
        ResultSet rs = null;
        boolean OK = true;
        // todo 密码加密 账户密码过滤
        String sql = "select * from admin where" + "AdminName=" + s1 + " and  AdminPwd=" + s2;
        rs = stmt.executeQuery(sql);// 执行sql语句
        if (!rs.next()) {
            OK = false;
        }
        return OK;
    }

    public List<AdminBean> getList(int page) {
        Connection conn = DBConn.getConn();
        String sql = "select * from admin limit " + (page - 1) * 10 + ",10";
        List<AdminBean> list = new ArrayList<>();
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                AdminBean AdminBean = new AdminBean();
                AdminBean.setAdminID(rs.getInt("adminID"));
                AdminBean.setAdminName(rs.getString("adminName"));
                AdminBean.setAdminPwd(rs.getString("adminPwd"));
                AdminBean.setAdminType(1);
                AdminBean.setLastLoginTime(Time.getDateTime());
                list.add(AdminBean);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    /**
     * @param adminBean 传入要更新的新闻对象,必须包含新闻id值
     * @return 收影响行数
     */
    public int update(AdminBean adminBean) {
        Connection conn = DBConn.getConn();
        if (adminBean.getAdminID() > 0) {
            try {
                Statement stmt = conn.createStatement();
                String sql = "update admin set adminPwd='" + adminBean.getAdminPwd() + "'"
                        + ",AdminType+'" + adminBean.getAdminType() + "'"
                        + ",AdminLastLoginTime='" + (Time.getDateTime()) + "'"//更新时使用当前系统时间
                        + ",AdminName='" + adminBean.getAdminName() + "'"//
                        + "where AdminID='" + adminBean.getAdminID() + "'";
                return stmt.executeUpdate(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return 0;
        }
        return 0;
    }

    public int add(AdminBean adminBean) {
        Connection conn = DBConn.getConn();
        try {//使用预处理执行插入
            String sql = "insert into admin(AdminName,AdminPwd,AdminType,AdminLastLoginTime) values(?,?,?,?)";
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, adminBean.getAdminName());//将第一个参数设置为标题内容
            pmst.setString(2, adminBean.getAdminPwd());
            pmst.setString(3, Time.getDateTime());
            pmst.setString(4, "临时管理员");
            return pmst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除功能
     */
    public int deleteNewsByAdminID(int adminID) {
        Connection conn = DBConn.getConn();
        String sql = "delete from admin where AdminID=?";
        try {
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setInt(1, adminID);
            return pmst.executeUpdate();
        } catch (SQLException e) {

        }
        return 0;
    }

    public int countPage() {
        Connection conn = DBConn.getConn();
        String sql = "select count(*) from admin";
        try {
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);//获取查询结果集
            if (rs.next()) {
                return (rs.getInt(1) / 10) + 1;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }

    public boolean CheckLogin(String user, String pwd) {
        return false;
    }
}